<?php
namespace Email;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
class  Email
{
    public $config ;

    protected $var;

    protected $base_data ;

    private $charset = 'GB2312';

    public function __construct($config)
    {
        $this->config['host'] = $config['host'] ;
        $this->config['port'] = $config['port'] ;
        $this->config['username'] = $config['username'] ;
        $this->config['password'] = $config['password'] ;
        $this->config['from_array'][0] = $config['username'];
        $this->config['smtp_tls'] = empty($config['smtp_tls'])?'':$config['smtp_tls'];
        if(empty($config['from_name']))
        {
            $this->config['from_array'][1] = 'MAIL' ;
        }
        else
        {
            $this->config['from_array'][1] = $this->charAction($config['from_name']) ;
        }
        if(empty($config['reply_to']))
        {
            $this->config['reply_to'] = array($config['username']);
        }
        elseif(is_array($config['reply_to']))
        {
            foreach($config['reply_to'] as $k=>$v)
            {
                $this->config['reply_to'][$k]= $this->charAction($v);
            }
            unset($k,$v);
        }
        else
        {
            $this->config['reply_to'][0] = $this->charAction($config['reply_to']) ;
            $this->config['reply_to'][] = $this->config['reply_to'][0] ;
        }
    }

    public function send($to , $title ,$content , $sign = false , $file = false)
    {
        $mail = new PHPMailer(true);                              // Passing `true` enables exceptions
        try {
            $mail->setLanguage('zh_cn');
            $mail->SMTPDebug = 0;
            $mail->isSMTP();
            $mail->Host = $this->config['host'];
            $mail->SMTPAuth = true;                               // Enable SMTP authentication
            $mail->Username = $this->config['username'];
            $mail->Password = $this->config['password'];
            if($this->config['smtp_tls'])
            {
                $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
            }
            else
            {
                $mail->SMTPAutoTLS = false;
            }
            $mail->Port = $this->config['port'];
            $mail->setFrom($this->config['from_array'][0],$sign?$sign:$this->config['from_array'][1]);
            $mail->isHTML(true);

            //Recipients
            if(!is_array($to))
            {
                $to = explode(',', trim(trim($to),','));
            }
            foreach ($to as $v)
            {
                $v = $this->make2ColumnArray($v);
                $mail->addAddress(...$v);
                $mail->addCC(...$v);
            }
            unset($v) ;
            if($this->config['reply_to'])
            {
                foreach($this->config['reply_to'] as $v)
                {
                    $v = $this->make2ColumnArray($v);
                    $mail->addReplyTo(...$v);
                }
                unset($v);
            }
            //$mail->addBCC('bcc@example.com');
            if($file)
            {
                if(!is_array($file))
                {
                    $file = array($file);
                }
                foreach ($file as $v)
                {
                    $v = $this->make2ColumnArray($v) ;
                    $mail->addAttachment(...$v);
                }
            }
            //Content
            $mail->Subject = $this->charAction($title);
            #$mail->AltBody = $this->charAction($desc ) ;
            $mail->Body    = $this->charAction($content) ;
            $mail->send();
            return false ;
        } catch (Exception $e) {
            return $mail->ErrorInfo." [error:{$e->getMessage()},line:{$e->getLine()},file:{$e->getFile()}]" ;
        }
    }

    /**
     * 将指定的编码字符串转换为中文编码
     * @param $string
     * @param string $code_charset
     * @return string
     */
    public function charAction($string,$code_charset = 'UTF-8')
    {
        return iconv($code_charset, "{$this->charset}//IGNORE", $string); ;
    }

    /**
     * @param $var
     * @return array
     */
    protected function make2ColumnArray($var)
    {
        if($var)
        {
            if(is_array($var))
            {
                reset($var);
                if(empty(next($var)))
                {
                    $var[] = prev($var) ;
                }
            }else
            {
                $var = array($var,$var);
            }
        }
        return $var ;
    }

    public function __set($name, $value)
    {
        $this->var[$name] = $value;
        // TODO: Implement __set() method.
    }

    public function __get($name)
    {
        if (array_key_exists($name, $this->var)) {
            return $this->var[$name];
        }
    }
}